<?php
if( !defined('CORE') ) exit('Request Error!');

/*
 *  公共部分-省部分代码
 * */
class mod
{
    public function get_page($where, $table, $order = null, $offset = 0, $page_size = 20, $is_report=0)
    {
        $where     = $where ?: '1=1';
        $order     = $order ? 'ORDER BY ' . $order : '';
        $table;     //= $table ?: $this->get_table();
        $offset    = intval($offset);
        $page_size = intval($page_size);

        $current_page = req::item('page_no', 0, 'int');
        $offset       = ($current_page - 1) > 0 ? ($current_page - 1) * $page_size : 0;

        $params_ = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
        parse_str($params_, $url_params);

        if (isset($url_params['page_no'])) {
            unset($url_params['page_no']);
        }

        //如果是导出就不要limit
        $limit = ($is_report==1)?'':"LIMIT {$offset}, {$page_size}";

        // 高效分页sql
        $sql   = "SELECT SQL_CALC_FOUND_ROWS * FROM {$table} WHERE {$where} {$order} {$limit}";
        $list  = db::get_all($sql);
        $row   = db::get_one("SELECT FOUND_ROWS() AS rowscount");
        $total = $row['rowscount'];

        $config['current_page'] = $current_page; // 当前页数,至少为1
        $config['page_size']    = $page_size; // 每页显示多少条
        $config['total_rs']     = $total; // 总记录数
        $config['url_prefix']   = '/?' . urldecode(http_build_query($url_params)); // 网址前缀
        $config['page_name']    = 'page_no'; // 当前分页变量名(默认是page_no,即访问是用 url_prefix&page_no=xxx )
        $config['move_size']    = 2; // 前后偏移量（默认是5）
        $config['input']        = 0; // 是否使用输入跳转框(0|1)
        $pages                  = util::pagination($config);
        return array($list, $pages);
    }

    public function arr_type()
    {
        $res = array();
        $list = db::get_all("select * from book_tag where id<100");
        if($list)foreach($list as $k=>$v)
        {
            $res[$v['id']] = $v['name'];
        }
        return $res;
    }

}
